﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MX.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records
{
    #region Usings

    using System;
    using System.Diagnostics.Contracts;
    using System.Globalization;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The MX record
    /// </summary>
    public sealed class MX : Record, IComparable
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MX"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public MX(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            this.Preference = recordReader.ReadUInt16();
            this.Exchange = recordReader.ReadDomainName();
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="MX" /> class.
        /// </summary>
        public MX()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Exchange.
        /// </summary>
        public string Exchange { get; private set; }

        /// <summary>
        ///     Gets Preference.
        /// </summary>
        public ushort Preference { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Compares to.
        /// </summary>
        /// <param name="obj">
        /// The obj A.
        /// </param>
        /// <returns>
        /// The compare to.
        /// </returns>
        public int CompareTo(object obj)
        {
            var recordMx = obj as MX;
            if (recordMx == null)
            {
                return -1;
            }

            if (this.Preference > recordMx.Preference)
            {
                return 1;
            }

            if (this.Preference < recordMx.Preference)
            {
                return -1;
            }

            // they are the same, now compare case insensitive names
            return string.Compare(this.Exchange, recordMx.Exchange, StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">
        /// The <see cref="System.Object"/> to compare with this instance.
        /// </param>
        /// <returns>
        /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }

            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            return obj.GetType() == typeof(MX) && this.Equals((MX)obj);
        }

        /// <summary>
        /// Equalses the specified other.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// Result stuff.
        /// </returns>
        public bool Equals(MX other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return Equals(other.Exchange, this.Exchange) && other.Preference == this.Preference;
        }

        /// <summary>
        ///     Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        ///     A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
        /// </returns>
        public override int GetHashCode()
        {
            unchecked
            {
                return ((this.Exchange != null ? this.Exchange.GetHashCode() : 0) * 397) ^ this.Preference.GetHashCode();
            }
        }

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "{0} {1}", this.Preference, this.Exchange);
        }

        #endregion
    }
}